/************************************************************************
 * @file AbstractPanelContainer.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <pluginui/manager/AbstractPanelContainer.h>

namespace antu
{
namespace plugin
{
namespace ui
{
AbstractPanelContainer::AbstractPanelContainer(const QString &name, Qt::DockWidgetArea area, Qt::DockWidgetAreas allowedAreas, QDockWidget::DockWidgetFeatures features)
    : m_sName(name),
      m_bChangeWindowTitleWithTab(false)
{
    this->set(area, allowedAreas, features);
}

AbstractPanelContainer::~AbstractPanelContainer()
{
    vector<PanelItem*>::iterator iter = this->m_oPanelItems.begin();
    vector<PanelItem*>::iterator end = this->m_oPanelItems.end();

    for (; iter != end; iter++)
    {
        if ((*iter)->widget()->parent() == nullptr)
        {
            delete (*iter)->widget();
        }
        delete (*iter);
    }
}

void AbstractPanelContainer::clear()
{
    this->m_eAllowedAreas = Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea;
    this->m_eFeatures = QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable;
    this->m_eInitialDockArea = Qt::NoDockWidgetArea;
    this->m_bOpenFloating = true;
}

void AbstractPanelContainer::set(Qt::DockWidgetArea area, Qt::DockWidgetAreas allowedAreas, QDockWidget::DockWidgetFeatures features)
{
    this->m_eAllowedAreas = allowedAreas;
    this->m_eFeatures = features;
    this->m_eInitialDockArea = area;
    this->m_bOpenFloating = (area == Qt::NoDockWidgetArea);
}

QString AbstractPanelContainer::containerName() const
{
    return this->m_sName;
}

void AbstractPanelContainer::addPanel(const QString &name, QWidget *pWidget, int stretch)
{
    PanelItem* pPanel = new PanelItem(name, pWidget);
    this->addToContainer(pPanel, stretch);
    this->m_oPanelItems.push_back(pPanel);
}

void AbstractPanelContainer::addPanel(const QString &name, QWidget *pWidget, QMenu *pContextMenu, int stretch)
{
    PanelItem* pPanel = new PanelItem(name, pWidget, pContextMenu);
    this->addToContainer(pPanel, stretch);
    this->m_oPanelItems.push_back(pPanel);
}

QStringList AbstractPanelContainer::panelNames() const
{
    QStringList result;
    vector<PanelItem*>::const_iterator iter = this->m_oPanelItems.begin();
    vector<PanelItem*>::const_iterator end = this->m_oPanelItems.end();

    for (; iter != end; iter++)
    {
        result.push_back((*iter)->widget()->objectName());
    }
    return result;
}

Qt::DockWidgetArea AbstractPanelContainer::dockArea() const
{
    return this->m_eInitialDockArea;
}

Qt::DockWidgetAreas AbstractPanelContainer::allowedAreas() const
{
    return this->m_eAllowedAreas;
}

QDockWidget::DockWidgetFeatures AbstractPanelContainer::features() const
{
    return this->m_eFeatures;
}

bool AbstractPanelContainer::isFloating() const
{
    return this->m_bOpenFloating;
}

PanelItem* AbstractPanelContainer::operator[](const QString& panelName)
{
    vector<PanelItem*>::iterator iter = this->m_oPanelItems.begin();
    vector<PanelItem*>::iterator end = this->m_oPanelItems.end();

    for (; iter != end; iter++)
    {
        if ((*iter)->widget()->objectName() == panelName)
            return (*iter);
    }
    return nullptr;
}

void AbstractPanelContainer::finalize()
{
    this->doFinalize();
}

void AbstractPanelContainer::doFinalize()
{

}
}
}
}

